package Gui.Dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Gui.DiceImagePanel;
import Gui.GameFrame;
import Main.Defines;
import PlayerManager.Chance;

public class LotteryDayDialog extends JDialog {
	private static final int LOTTERY_TICKET_COST = 100;
	private JPanel jp; 
	public void buildUIContents() {
		// set info label
		JLabel lotteryInfo = new JLabel(
				"<html>Please select the users who want "
						+ "<br></br>to participate in the lottery. </html>");
		jp.add(lotteryInfo, BorderLayout.NORTH);

		// show player checkboxes
		final ArrayList<JCheckBox> boxList;
		ButtonGroup group = new ButtonGroup();
		ArrayList<JCheckBox> localBoxList = new ArrayList<JCheckBox>();
		for (int i = 0; i < Defines.GM.getNumberOfPlayers(); ++i) {
			JCheckBox playerBox = new JCheckBox(String.format("Player %d",
					i + 1));
			group.add(playerBox);
			jp.add(playerBox);
			localBoxList.add(playerBox);
		}
		
		boxList = localBoxList;
		// show ok button
		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				GameFrame.getInstance().update();
				boolean someoneParticipating = false;
				for (JCheckBox a : boxList) {
					if (a.isSelected())
						someoneParticipating = true;
				}
				if (someoneParticipating) {
					LotterDayDialogAfterMath showDialogAfterMath = new LotterDayDialogAfterMath(
							boxList);
				}

			}
		});
		jp.add(ok);
	}

	public LotteryDayDialog() {
		super(GameFrame.getInstance(), "Lottery Dialog", true);
		//setLayout(new GridLayout(0, 1, 0, 5));
		jp = new JPanel();
		jp.setLayout(new BorderLayout());
		setLocationRelativeTo(GameFrame.getInstance());
		buildUIContents();
		add(jp);
		pack();
		
		// REFACTOR THIS! MAGIC NUMBER
		if(getTitle() != null){
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + 75; // MAGIC NUMBER, 75 is size of a "typical" window icon
			width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}
		setVisible(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

	}

	private class LotterDayDialogAfterMath extends JDialog {
		private void buildUIContents(ArrayList<JCheckBox> boxList) {
			Chance chance = new Chance();

			boolean aPlayerWonLotto = false;
			// set so eclipse won't complain that it hasnt been initialized
			int whichPlayerWon = -1;
			// this makes sure a player is playing so that 1000 is added to the
			// lottery pile
			boolean atLeastOnePlayerIsPlaying = false;
			String allText = "";
			JLabel allTextLabel = new JLabel();
			
			for (int i = 0; i < boxList.size(); ++i) {
				if (boxList.get(i).isSelected()) {
					atLeastOnePlayerIsPlaying = true;
					// automatically take out a loan
					// WARNING DEBUG! MUST CHANGE
					Defines.PLM.getPlayerList().get(i).setCash(0);
					if (Defines.PLM.getPlayerList().get(i).getCash() < LOTTERY_TICKET_COST) {
						// maybe show dialog asking if they want to loan
						
						AskForLoanDialog showDialog = new AskForLoanDialog(i, LOTTERY_TICKET_COST, " for the lottery ticket", 1000);// MAGIC NUMBER
						if(showDialog.isClosed() && showDialog.isLoanOK()){
							Defines.PLM.getPlayerList().get(i).AddCash(
									-LOTTERY_TICKET_COST);
							Defines.GM.addToLotteryPile(LOTTERY_TICKET_COST);
							Defines.PLM.getPlayerList().get(i).AddCash(1000);
							Defines.PLM.getPlayerList().get(i).AddLoan(1000);
						}
					}
					else{
					// remove 100 dollars from their cash and add to lottery
						Defines.PLM.getPlayerList().get(i).AddCash(
								-LOTTERY_TICKET_COST);
						Defines.GM.addToLotteryPile(LOTTERY_TICKET_COST);
					}
					// check iterated player's lotto card if it has a matching
					// 2d roll
					if (Defines.PLM.getPlayerList().get(i).getLottoCard()
							.contains(chance)) {
						aPlayerWonLotto = true;
						whichPlayerWon = i;
					}

				}
			}
			if (atLeastOnePlayerIsPlaying) {
				// 1000 dollars is added to pile from bank
				Defines.GM.addToLotteryPile(1000); // MAGIC NUMBER
				// if a player won the lottery, print it, add lottery money and
				// set pile to 0
				
				//JLabel lottoRoll = new JLabel(String.format("The Lotto Roll is %s", chance.toString()));
				allText += String.format("<html>The Lotto Roll is %s<br>", chance.toString());
				//add(lottoRoll);
				
				JPanel rollPics = new JPanel();
				try {
					rollPics.add(new DiceImagePanel().getDicePanel(false, chance.getFirst()));
					rollPics.add(new DiceImagePanel().getDicePanel(true, chance.getSecond()));
					add(rollPics);
				}
				
				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (aPlayerWonLotto) {
//					JLabel playerWonLotto = new JLabel(String.format(
//							"Player %d WON $%d", whichPlayerWon + 1, Defines.GM
//									.getLotteryPile()));
					allText += String.format("<br>Player %d WON $%d<br>", whichPlayerWon + 1, Defines.GM
									.getLotteryPile()); 
					Defines.PLM.getPlayerList().get(whichPlayerWon).AddCash(
							Defines.GM.getLotteryPile());
					Defines.GM.setLotteryPile(0);// MAGIC NUMBER
//					add(playerWonLotto);
					GameFrame.getInstance().update();
				}
				else {
//					JLabel lotteryAddedInfo = new JLabel(
//							"No one won lottery this week.");
					allText += String
							.format("No one won the lottery this week.<br>" +
									"");
					allText += String.format("<br>Jackpot value: %d<br>", Defines.GM
							.getLotteryPile());
//					JLabel jackpotInfo = new JLabel(String.format(
//							"Jackpot value: %d", Defines.GM.getLotteryPile()));
//					add(lotteryAddedInfo);
//					add(jackpotInfo);
					GameFrame.getInstance().update();
				}

			}
			allTextLabel.setText(allText);
			add(allTextLabel);
			// add ok button
			JButton ok = new JButton("OK");
			ok.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					dispose();

				}
			});
			add(ok);
		}

		public LotterDayDialogAfterMath(ArrayList<JCheckBox> boxList) {
			super(GameFrame.getInstance(), "Lottery Dialog", true);
			setLayout(new GridLayout(0,1,0,0));
			setLocationRelativeTo(GameFrame.getInstance());
			buildUIContents(boxList);
			
			
			pack();
			setVisible(true);
			setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			
		}
	}
}
